package hot100.logos11To20;

/**
 * @author logos
 * date 2025/7/10 16:41
 * @version v1.0
 * @Package hot100
 */
public class logos16 {


    /**
     * 16. 除自身以外数组的乘积
     * https://leetcode.cn/problems/product-of-array-except-self/description/?envType=study-plan-v2&envId=top-100-liked
     */
    class Solution {
        public int[] productExceptSelf(int[] nums) {
            int n = nums.length;
            int[] ans = new int[n];
            ans[n - 1] = 1;
            for (int i = n - 2; i >= 0; i--) {
                ans[i] = ans[i + 1] * nums[i + 1];
            }
            int pre = 1;
            for (int i = 0; i < n; i++) {
                ans[i] *= pre;
                pre *= nums[i];
            }
            return ans;
        }
    }
}
